package com.youtui.manager.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youtui.common.constant.ResultMessage;
import com.youtui.common.constant.SystemConstant;
import com.youtui.common.util.Md5Util;
import com.youtui.common.util.ResultUtil;
import com.youtui.manager.model.Developer;
import com.youtui.manager.service.IDeveloperService;
import com.youtui.manager.service.ISmsCodeService;

@Controller
@RequestMapping("/developer")
public class DeveloperController extends BaseController {

	@Autowired
	IDeveloperService developerService;
	@Autowired
	ISmsCodeService smsCodeService;

	@RequestMapping("/add.do")
	@ResponseBody
	public ResultMessage add(Developer dev, String smsCode) {
		if (StringUtils.isBlank(smsCode)) {
			return ResultUtil.result(ResultUtil.code_fail, "验证码不能为空");
		}
		if (StringUtils.isBlank(dev.getLoginName())) {
			return ResultUtil.result(ResultUtil.code_fail, "登录名不能为空");
		}
		if (StringUtils.isBlank(dev.getCompanyName())) {
			return ResultUtil.result(ResultUtil.code_fail, "公司名称不能为空");
		}
		if (StringUtils.isBlank(dev.getAppName())) {
			return ResultUtil.result(ResultUtil.code_fail, "应用名称不能为空");
		}
		if (StringUtils.isBlank(dev.getMobile()) || dev.getMobile().length() != 11) {
			return ResultUtil.result(ResultUtil.code_fail, "手机号格式错误");
		}

		boolean flag = smsCodeService.checkSmsCode(dev.getMobile(), smsCode, SystemConstant.SmsCode_Type_login);
		if (!flag) {
			return ResultUtil.result(ResultUtil.code_fail, "短信验证码错误");
		}

		Developer devtmp = developerService.findDeveloperByLoginName(dev.getLoginName());
		if (devtmp != null) {
			return ResultUtil.result(ResultUtil.code_fail, "该登录名已被使用");
		}
		
		devtmp = developerService.findDeveloperByMobile(dev.getMobile());
		if (devtmp != null) {
			return ResultUtil.result(ResultUtil.code_fail, "该手机号已被使用");
		} 

		developerService.developerAdd(dev);
		return ResultUtil.result(ResultUtil.code_success, "注册成功");

	}

	@RequestMapping("/load.do")
	@ResponseBody
	public ResultMessage list(Long id) {

		if (id == null) {
			return ResultUtil.result(ResultUtil.code_fail, "开发者详情获取失败,参数错误");
		}

		Developer developer = developerService.findDeveloperById(id);
		if (developer != null) {
			return ResultUtil.result(ResultUtil.code_success, "开发者详情", developer);
		}
		return ResultUtil.result(ResultUtil.code_fail, "开发者详情获取失败");
	}

	@RequestMapping("/update.do")
	@ResponseBody
	public ResultMessage update(Developer developer) {

		if (developer == null || developer.getId() == null) {
			return ResultUtil.result(ResultUtil.code_fail, "参数错误");
		}
		if (StringUtils.isNotBlank(developer.getMobile())  && developer.getMobile().length() != 11) {
			return ResultUtil.result(ResultUtil.code_fail, "手机号格式错误");
		}
		
		Developer dev = developerService.findDeveloperById(developer.getId());
		if(!dev.getMobile().equals(developer.getMobile())){
			Developer devtmp = developerService.findDeveloperByMobile(developer.getMobile());
			if (devtmp!=null ) {
				return ResultUtil.result(ResultUtil.code_fail, "该手机号已被使用");
			}
		}
		developerService.update(developer);

		return ResultUtil.result(ResultUtil.code_success, "开发者信息更新成功");
	}

	@RequestMapping("/update_pwd.do")
	@ResponseBody
	public ResultMessage updatePwd(Long id, String oldPwd, String newPwd) {

		if (id == null || StringUtils.isBlank(newPwd) || StringUtils.isBlank(oldPwd)) {
			return ResultUtil.result(ResultUtil.code_fail, "请求参数错误");
		}
		if (oldPwd.equals(newPwd)) {
			return ResultUtil.result(ResultUtil.code_fail, "修改前后密码不能一致");
		}

		Developer developer = developerService.findDeveloperById(id);
		if (developer == null) {
			return ResultUtil.result(ResultUtil.code_fail, "开发者信息不存在");
		}

		if (!developer.getPassword().equals(Md5Util.md5(oldPwd))) {
			return ResultUtil.result(ResultUtil.code_fail, "原始密码错误");
		}

		developer.setPassword(Md5Util.md5(newPwd));
		developerService.update(developer);

		return ResultUtil.result(ResultUtil.code_success, "密码修改成功");
	}

	@RequestMapping("/reset_pwd.do")
	@ResponseBody
	public ResultMessage resetPwd(String loginName, String mobile, String smsCode, String pwd) {

		if (StringUtils.isBlank(loginName) || StringUtils.isBlank(mobile) || StringUtils.isBlank(smsCode)
				|| StringUtils.isBlank(pwd)) {
			return ResultUtil.result(ResultUtil.code_fail, "请求参数错误");
		}

		Developer dev = developerService.findDeveloperByLoginName(loginName);
		if (dev == null) {
			return ResultUtil.result(ResultUtil.code_fail, "该用户信息不存在");
		}
		
		if(!dev.getMobile().equals(mobile)){
			return ResultUtil.result(ResultUtil.code_fail, "该手机号与注册手机号不一致");
		}
		
		boolean flag = smsCodeService.checkSmsCode(dev.getMobile(), smsCode, SystemConstant.SmsCode_Type_login);

		if (!flag) {
			return ResultUtil.result(ResultUtil.code_fail, "短信验证码错误");
		}
 
		dev.setPassword(Md5Util.md5(pwd));
		developerService.update(dev);

		return ResultUtil.result(ResultUtil.code_success, "密码重置成功");
	}

}
